<?php
require_once("myheader.php");
require_once("myfooter.php");
require_once("helper/connexion.php");
require_once("helper/enseignement_helper.php");
myHeader("enseignements");
connexion_sql();

// http://www.info-3000.com/phpmysql/validation/

// action : add / edit
if(isset($_GET['action']) &&  !empty($_GET['action']))
{
  $action = $_GET['action'];
  
  if($action == 'edit' &&  isset($_GET['id'])) {
     $id = (int) $_GET['id'];
	 $data = pg_fetch_assoc(getEnseignementById($id)); 
	}  
 
}
//if(isset($POST['input']))
//	$ = $_POST['input'];

?>
<script type="text/javascript">
$(function()
{
    $('#inputDateStart').datepicker({format: 'yyyy-mm-dd'});
    $('#inputDateEnd').datepicker({format: 'yyyy-mm-dd'});


// disabling dates
var nowTemp = new Date();
var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

var checkin = $('#inputDateStart').datepicker({
          onRender: function(date) {
            return date.valueOf() < now.valueOf() ? 'disabled' : '';
          }
        }).on('changeDate', function(ev) {
          if (ev.date.valueOf() > checkout.date.valueOf()) {
            var newDate = new Date(ev.date)
            newDate.setDate(newDate.getDate() + 1);
            checkout.setValue(newDate);
          }
          checkin.hide();
          $('#dpd2')[0].focus();
        }).data('datepicker');
        var checkout = $('#dpd2').datepicker({
          onRender: function(date) {
            return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
          }
        }).on('changeDate', function(ev) {
          checkout.hide();
        }).data('datepicker');
		});


function hide(idButton, idDiv) {
        var toggle_switch = $(this);
        $(idDiv).toggle(function(){
	  if($(this).css('display')=='none'){
     	    toggle_switch.html('Show');	    
	    $(idButton+' i').removeClass('icon-minus').addClass('icon-plus');
	  }else{
     	    toggle_switch.html('Hide');
	    $(idButton+' i').removeClass('icon-plus').addClass('icon-minus');
	  }
	});
}

   $(document).ready(function(){
     hide('#hideFormationButton', '#hideFormation');
     hide('#hideOptionsButton', '#hideOptions');
   });

</script>


<div class="container">

<div id="body_gestion">
<?php 
if(isset($action))
{
	if($action == 'edit' &&  isset($_GET['id']))
	{
		echo '<h1>Modifier "' . $data['nom'] . '"</h1>';
	}
	else
	{
		echo '<h1>Ajouter un enseignement</h1>';
		
	}
}
?>
<br><br>


<!-- FORM -->


<form class=" form-horizontal" method="post">

<?php
if(isset($_POST["valid"]) &&  !is_null($_POST["valid"]))
{
  updateEnseignement($_POST['id'],$_POST["inputIntitule"], $_POST["inputCode"], $_POST["inputCours"], $_POST["inputTD"], $_POST["inputDateDebut"], $_POST["inputDateEnd"], $_POST["inputOptions"], $_POST["inputUe"]); 	
}

?>

<input type="hidden" id="id" name="id" value=<?php echo $id ;  ?> />

<div class="control-group">
     <label class="control-label" for="inputIntitule">Intitulé</label>
     <div class="controls">
     	  <input type="text" id="Intitule" value="<?php echo ((isset($data['nom'])) ? $data['nom'] : '');  ?>" name="inputIntitule"/>
     </div>
</div>
<div class="control-group">
     <label class="control-label" for="inputCode">Code</label>
     <div class="controls">
     	  <input class="input-mini" type="text" id="inputCode" value="<?php echo ((isset($data['code'])) ? $data['code'] : '');  ?>" name="inputCode"/>
     </div>
</div>

<div class="control-group">
  <label class="control-label" for="inputDateStart">Date de début</label>
  <div class="controls">
     <input type="text" class="span2" value="<?php echo ((isset($data['debut'])) ? $data['debut'] : '');  ?>" id="inputDateStart" name="inputDateStart"/>
  </div>
</div>

<div class="control-group">
  <label class="control-label" for="inputDateEnd">Date de fin</label>
  <div class="controls">
     <input type="text" class="span2" value="<?php echo ((isset($data['fin'])) ? $data['fin'] : '');  ?>" id="inputDateEnd" name="inputDateEnd"/>
  </div>
</div>


<h4>	Nombre d'heures </h4>


<div class="control-group">
     <label class="control-label" for="inputCours">Cours</label>
     <div class="controls">
     	  <input type="text" id="inputCours" name="inputCours" value="<?php echo ((isset($data['heurecm'])) ? $data['heurecm'] : '');  ?>"/>
     </div>
</div>

<div class="control-group">
     <label class="control-label" for="inputTD">TD</label>
     <div class="controls">
     	  <input type="text" id="inputTD" value="<?php echo ((isset($data['heuretd'])) ? $data['heuretd'] : '');  ?>" name="inputTD"/>
     </div>
</div>

<div class="control-group">
	<div class="controls">
		<table class="table table-hover table-bordered table-condensed" style="width:200px;">
			<thead>
				<tr><th>UE</th><tr><!--<th>Heures</th>-->
			</thead>
			<tbody>
				<tr>
					<td>
							<select name="inputUe" id="inputUe"/>
							<?php
								$req = getAllUe();
								while($row = pg_fetch_assoc($req))
								{
									if($data['idue'] == $row["idue"] )
									{
										echo '<option name="inputUe" value="'.$row["idue"].'" selected >'.$row["nomue"].'</option>';
									}
									else
									{
										echo '<option name="inputUe" value="'.$row["idue"].'">'.$row["nomue"].'</option>';
									}
									
										
								}
							?>
					</td>
					<td>
						<!--<input class="input-mini" type="text" id="inputHrUe" value="<?php echo ((isset($data['nbHrUe'])) ? $data['nbHrUe'] : '');  ?>" name="inputHrUe"/>-->
					</td>
				</tr>
			</tboby>
		</table>
	</div>
</div>

<div class="control-group">
     <label class="control-label" for="inputFormation">Formation</label>
     <div class="controls">
		<select name="inputFormation" id="inputFormation"/>
	<?php
		$req = getFormation();
		while($row = pg_fetch_assoc($req))
		{
				echo '<option name="inputFormation" value="'.$row["idformation"].'">'.$row["nomformation"].'</option>';
		}
	?>
		</select>
	  <button class="btn" type="button" id="hideFormationButton" onclick="hide('#hideFormationButton', '#hideFormation');" ><i class="icon-plus"></i></button>
	  <div id="hideFormation">
	      TEST
	 </div>
     </div>
</div>
<div class="control-group">
     <label class="control-label" for="inputOptions">Options</label>
     <div class="controls">
		<select id="inputOptions" name="inputOptions"/>
		<option name="inputOption" value=""></option>	
		
		<?php
						$req = getAllOption();
						while($row = pg_fetch_assoc($req))
						{
							if($data['idoption'] == $row["idoption"] )
							{
								echo '<option name="inputOption" value="'.$row["idoption"].'" selected>'.$row["nomoption"].'</option>';
							}
							else
							{
								echo '<option name="inputOption" value="'.$row["idoption"].'">'.$row["nomoption"].'</option>';
							}
							
								
						}
	  ?>

	  </select>
	  <button class="btn" type="button" id="hideOptionsButton" onclick="hide('#hideOptionsButton', '#hideOptions');" ><i class="icon-plus"></i></button>
	  <div id="hideOptions">
	      Test options
	 </div>
     </div>
</div>

<div class="control-group">
     <div class="form-actions">
     	  <button type="submit" class="btn btn-primary" name="valid">Valider</button>
	  <button type="reset" class="btn">Annuler</button>
     </div>
</div>
</form>

</div>

</div>
<?php
myFooter();
?>

